//
//  MILMyCoderViewController.m
//  MicrowaveInteractionLight
//
//  Created by maple on 2016/12/15.
//  Copyright © 2016年 maple. All rights reserved.
//

#import "MILMyCoderViewController.h"
#import <CoreImage/CoreImage.h>
#import "UIButton+WebCache.h"

@interface MILMyCoderViewController ()

@property (weak, nonatomic) IBOutlet UIImageView *myImageView;
@property (weak, nonatomic) IBOutlet UIButton *iconBtn;
@property (weak, nonatomic) IBOutlet UIButton *nicknameLabel;

@end

@implementation MILMyCoderViewController
#pragma mark -- UI
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    //初始化UI
    [self initMilMycoderView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)refreshBaseControlValue
{
    [self setNavigationBarTitle:@"二维码名片"];
    self.navigationBar.backgroundColor = [UIColor blackColor];
    [self setLeftButtonImage:[UIImage imageNamed:@"back_icon"]];
}
//初始化UI
- (void)initMilMycoderView
{
    //剪切圆角
    self.iconBtn.layer.cornerRadius = 48.5;
    self.iconBtn.clipsToBounds = YES;
    if (EXRACT_OBJECT(ICON) && ![EXRACT_OBJECT(ICON) isEqualToString:@""])
    {
        [self.iconBtn sd_setImageWithURL:[NSURL URLWithString:EXRACT_OBJECT(ICON)] forState:UIControlStateNormal];
        
    }
    else
    {
        [self.iconBtn setBackgroundImage:[UIImage imageNamed:@"touxiang"] forState:UIControlStateNormal];
    }

    [self.nicknameLabel setTitle:EXRACT_OBJECT(NICKNAME) forState:UIControlStateNormal];
    // 1.创建过滤器
    CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
    // 2.恢复默认
    [filter setDefaults];
    // 3.给过滤器添加数据
//    NSDictionary *mydic = @{@"uid":EXRACT_OBJECT(UID),@"token":EXRACT_OBJECT(TOKEN),@"mobile":EXRACT_OBJECT(USERNAME)};
//    NSData *data=[NSJSONSerialization dataWithJSONObject:mydic options:NSJSONWritingPrettyPrinted error:nil];
    
    NSString *dataString = EXRACT_OBJECT(UID);
    NSData *data = [dataString dataUsingEncoding:NSUTF8StringEncoding];
    // 4.通过KVO设置滤镜inputMessage数据
    [filter setValue:data forKeyPath:@"inputMessage"];
    // 4.获取输出的二维码
    CIImage *outputImage = [filter outputImage];
    // 5.将CIImage转换成UIImage，并放大显示
    self.myImageView.image = [self createNonInterpolatedUIImageFormCIImage:outputImage withSize:100];
}

- (UIImage *)createNonInterpolatedUIImageFormCIImage:(CIImage *)image withSize:(CGFloat) size {
    
    CGRect extent = CGRectIntegral(image.extent);
    
    CGFloat scale = MIN(size/CGRectGetWidth(extent), size/CGRectGetHeight(extent));
    
    // 创建bitmap;
    
    size_t width = CGRectGetWidth(extent) * scale;
    
    size_t height = CGRectGetHeight(extent) * scale;
    
    CGColorSpaceRef cs = CGColorSpaceCreateDeviceGray();
    
    CGContextRef bitmapRef = CGBitmapContextCreate(nil, width, height, 8, 0, cs, (CGBitmapInfo)kCGImageAlphaNone);
    
    CIContext *context = [CIContext contextWithOptions:nil];
    
    CGImageRef bitmapImage = [context createCGImage:image fromRect:extent];
    
    CGContextSetInterpolationQuality(bitmapRef, kCGInterpolationNone);
    
    CGContextScaleCTM(bitmapRef, scale, scale);
    
    CGContextDrawImage(bitmapRef, extent, bitmapImage);
    
    // 保存bitmap到图片
    
    CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef);
    
    CGContextRelease(bitmapRef);
    
    CGImageRelease(bitmapImage);
    
    return [UIImage imageWithCGImage:scaledImage];
    
}

@end
